﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;

public class Example2 : MonoBehaviour
{
    public Animator animator;
    public AnimationClip blendClip1;
    [Range(0, 1)]
    public float weight1 = 0.5f;
    public AnimationClip blendClip2;
    [Range(0, 1)]
    public float weight2 = 0.5f;

    PlayableGraph mPlayableGraph;
    AnimationMixerPlayable mMixedPlayable;


    void OnEnable()
    {
        mPlayableGraph = new PlayableGraph();
        mMixedPlayable = AnimationPlayableUtilities.PlayMixer(animator, 2, out mPlayableGraph);

        var clipPlayable1 = AnimationClipPlayable.Create(mPlayableGraph, blendClip1);
        var clipPlayable2 = AnimationClipPlayable.Create(mPlayableGraph, blendClip2);

        mPlayableGraph.Connect(clipPlayable1, 0, mMixedPlayable, 0);
        mPlayableGraph.Connect(clipPlayable2, 0, mMixedPlayable, 1);

        mPlayableGraph.Play();
    }

    void Update()
    {
        if (!mMixedPlayable.CanSetWeights()) return;

        mMixedPlayable.SetInputWeight(0, weight1);
        mMixedPlayable.SetInputWeight(1, weight2);
    }

    void OnDestroy()
    {
        mPlayableGraph.Destroy();
    }
}
